'use strict';

const { Controller } = require('egg');
const scopes = require('../data/scope');

class ScopeController extends Controller {
  async submit() {
    const { ctx } = this;
    ctx.validate({
      scope: {
        type: 'string',
        allowEmpty: true,
      },
    });
    const scope = ctx.request.body.scope.split(' ');
    for (const value of scope) {
      if (scopes.indexOf(value) === -1 && value !== '') {
        ctx.status = 404;
        return;
      }
    }
    ctx.session.scope = ctx.request.body.scope;
    ctx.status = 204;
  }
}

module.exports = ScopeController;
